starting a concerns pattern with a WeiboConern. concerns can encapsulate common functionality between related agents

Albert Sun 11 years ago
parent
commit
88043dc348

+ 29 - 0
app/concerns/weibo_concern.rb

@@ -0,0 +1,29 @@
1
+module WeiboConcern
2
+  extend ActiveSupport::Concern
3
+
4
+  included do
5
+    self.validate :validate_weibo_options
6
+  end
7
+
8
+  def validate_weibo_options
9
+    unless options[:app_key].present? &&
10
+        options[:app_secret].present? &&
11
+        options[:access_token].present?
12
+        errors.add(:base, "app_key, app_secret and access_token are required")
13
+    end
14
+  end
15
+
16
+  def weibo_client
17
+    unless @weibo_client
18
+      WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY
19
+      WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET
20
+      @weibo_client = WeiboOAuth2::Client.new
21
+      @weibo_client.get_token_from_hash :access_token => options[:access_token]
22
+    end
23
+    @weibo_client
24
+  end
25
+
26
+  module ClassMethods
27
+
28
+  end
29
+end

+ 5 - 11
app/models/agents/weibo_publish_agent.rb

@@ -3,6 +3,8 @@ require "weibo_2"
3 3
 
4 4
 module Agents
5 5
   class WeiboPublishAgent < Agent
6
+    include WeiboConcern
7
+
6 8
     cannot_be_scheduled!
7 9
 
8 10
     description <<-MD
@@ -19,11 +21,8 @@ module Agents
19 21
 
20 22
     def validate_options
21 23
       unless options[:uid].present? &&
22
-        options[:expected_update_period_in_days].present? &&
23
-        options[:app_key].present? &&
24
-        options[:app_secret].present? &&
25
-        options[:access_token].present?
26
-        errors.add(:base, "expected_update_period_in_days, uid, and access_token are required")
24
+        options[:expected_update_period_in_days].present?
25
+        errors.add(:base, "expected_update_period_in_days and uid are required")
27 26
       end
28 27
     end
29 28
 
@@ -73,12 +72,7 @@ module Agents
73 72
     end
74 73
 
75 74
     def publish_tweet text
76
-      WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY
77
-      WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET
78
-      client = WeiboOAuth2::Client.new
79
-      client.get_token_from_hash :access_token => options[:access_token]
80
-
81
-      client.statuses.update text
75
+      weibo_client.statuses.update text
82 76
     end
83 77
 
84 78
     def unwrap_tco_urls text, tweet_json

+ 5 - 12
app/models/agents/weibo_user_agent.rb

@@ -3,6 +3,8 @@ require "weibo_2"
3 3
 
4 4
 module Agents
5 5
   class WeiboUserAgent < Agent
6
+    include WeiboConcern
7
+
6 8
     cannot_receive_events!
7 9
 
8 10
     description <<-MD
@@ -69,11 +71,8 @@ module Agents
69 71
 
70 72
     def validate_options
71 73
       unless options[:uid].present? &&
72
-        options[:expected_update_period_in_days].present? &&
73
-        options[:app_key].present? &&
74
-        options[:app_secret].present? &&
75
-        options[:access_token].present?
76
-        errors.add(:base, "expected_update_period_in_days, uid, app_key, app_secret and access_token are required")
74
+        options[:expected_update_period_in_days].present?
75
+        errors.add(:base, "expected_update_period_in_days and uid are required")
77 76
       end
78 77
     end
79 78
 
@@ -92,18 +91,12 @@ module Agents
92 91
     end
93 92
 
94 93
     def check
95
-      WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY
96
-      WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET
97
-      client = WeiboOAuth2::Client.new
98
-      client.get_token_from_hash :access_token => options[:access_token]
99
-
100
-
101 94
       since_id = memory[:since_id] || nil
102 95
       opts = {:uid => options[:uid].to_i}
103 96
       opts.merge! :since_id => since_id unless since_id.nil?
104 97
 
105 98
       # http://open.weibo.com/wiki/2/statuses/user_timeline/en
106
-      resp = client.statuses.user_timeline opts
99
+      resp = weibo_client.statuses.user_timeline opts
107 100
       if resp[:statuses]
108 101
 
109 102